home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / dev / lang / Python16_Src.lha / Python16_Source / Python / importdl.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-08-03  |  2.0 KB  |  82 lines

  1. /* Support for dynamic loading of extension modules */
  2.  
  3. #include "Python.h"
  4.  
  5. /* ./configure sets HAVE_DYNAMIC_LOADING if dynamic loading of modules is
  6.    supported on this platform. configure will then compile and link in one
  7.    of the dynload_*.c files, as appropriate. We will call a function in
  8.    those modules to get a function pointer to the module's init function.
  9. */
  10. #ifdef HAVE_DYNAMIC_LOADING
  11.  
  12. #include "importdl.h"
  13.  
  14. extern dl_funcptr _PyImport_GetDynLoadFunc(const char *name,
  15.                        const char *shortname,
  16.                        const char *pathname, FILE *fp);
  17.  
  18.  
  19.  
  20. PyObject *
  21. _PyImport_LoadDynamicModule(name, pathname, fp)
  22.     char *name;
  23.     char *pathname;
  24.     FILE *fp;
  25. {
  26.     PyObject *m, *d, *s;
  27.     char *lastdot, *shortname, *packagecontext;
  28.     dl_funcptr p;
  29.  
  30.     if ((m = _PyImport_FindExtension(name, pathname)) != NULL) {
  31.         Py_INCREF(m);
  32.         return m;
  33.     }
  34.     lastdot = strrchr(name, '.');
  35.     if (lastdot == NULL) {
  36.         packagecontext = NULL;
  37.         shortname = name;
  38.     }
  39.     else {
  40.         packagecontext = name;
  41.         shortname = lastdot+1;
  42.     }
  43.  
  44.     p = _PyImport_GetDynLoadFunc(name, shortname, pathname, fp);
  45.     if (PyErr_Occurred())
  46.         return NULL;
  47.     if (p == NULL) {
  48.         PyErr_Format(PyExc_ImportError,
  49.            "dynamic module does not define init function (init%.200s)",
  50.                  shortname);
  51.         return NULL;
  52.     }
  53.     _Py_PackageContext = packagecontext;
  54.     (*p)();
  55.     _Py_PackageContext = NULL;
  56.     if (PyErr_Occurred())
  57.         return NULL;
  58.     if (_PyImport_FixupExtension(name, pathname) == NULL)
  59.         return NULL;
  60.  
  61.     m = PyDict_GetItemString(PyImport_GetModuleDict(), name);
  62.     if (m == NULL) {
  63.         PyErr_SetString(PyExc_SystemError,
  64.                 "dynamic module not initialized properly");
  65.         return NULL;
  66.     }
  67.     /* Remember the filename as the __file__ attribute */
  68.     d = PyModule_GetDict(m);
  69.     s = PyString_FromString(pathname);
  70.     if (s == NULL || PyDict_SetItemString(d, "__file__", s) != 0)
  71.         PyErr_Clear(); /* Not important enough to report */
  72.     Py_XDECREF(s);
  73.     if (Py_VerboseFlag)
  74.         PySys_WriteStderr(
  75.             "import %s # dynamically loaded from %s\n",
  76.             name, pathname);
  77.     Py_INCREF(m);
  78.     return m;
  79. }
  80.  
  81. #endif /* HAVE_DYNAMIC_LOADING */
  82.